<?php
/**
 * 预警
 * @author wyb
 */
namespace Qwadmin\Controller;
class WarningController extends ComController {
	
	/**
	 * 库存预警
	 */
	public function stock() {
		//$page = I('get.p') ? I('get.p') : 1;
		$data = M('stock_warning')->alias('sw')->join('__PURCHORDER_LINE__ pl ON pl.sku = sw.sku')
								  ->field('distinct sw.sku,pl.earn13,(SELECT SUM(num) FROM `zt_purchorder_line` WHERE `sku` = pl.sku) AS num,pl.name,sw.info,sw.check_time')->order('sw.check_time DESC')
								  ->select();
		//$total = M('stock_warning')->alias('sw')->join('__PURCHORDER_LINE__ pl ON pl.sku = sw.sku')->count();
		//$this->__assignPage( $total );
        $this->assign( 'lists' , $data );
		$this->display();
	}

    /**
     * 销售订单
     * @return [type] [description]
     */
    public function getData() {
        $sku = I('get.sku') ? I('get.sku') : exit( json_encode(array('msg' => '信息错误' , 'status' => 0 ) ) );
        $data = M('order_line')->alias('ol')->join('__ORDER__ o ON o.id = ol.order_id')->where( array( 'ol.sku' => $sku ) )->field('o.ref,ol.price,ol.date_order,o.buyer,o.channel_code,ol.num,o.trade_pattern')->order('ol.date_order DESC')->select();
        exit( json_encode(array('data' => $data , 'status' => 1 ) ) );
    }

    /**
     * 分拣单
     * @return [type] [description]
     */
    public function getRmData() {
    	$sku = I('get.sku') ? I('get.sku') : exit( json_encode(array('msg' => '信息错误' , 'status' => 0 ) ) );
        $data = M('stock_moveline')->where( array( 'sku' => $sku ) )->order('`date` DESC')->field('`picking_id`,`qty_act`,`location_id`,`location_dest_id`,`procure_method`,`state`,`date`')->select();
        //echo M('stock_moveline')->getLastSql();die;
        if( $data ) {
	        foreach( $data as $key => $value ) {
	        	$data[$key]['date'] = date('Y-m-d H:i:s' , $value['date']);
	        	$data[$key]['location_id'] = getStockStatus( $value['location_id'] );
	        	$data[$key]['location_dest_id'] = getStockStatus( $value['location_dest_id'] );
	        }
    	}
        exit( json_encode(array('data' => $data , 'status' => 1 ) ) );
    }

    /**
     * 采购单
     * @return [type] [description]
     */
    public function getPurchaseList() {
    	$sku = I('get.sku') ? I('get.sku') : exit( json_encode(array('msg' => '信息错误' , 'status' => 0 ) ) );
    	$data = M('purchorder_line')->alias('pl')->join('__PURCHORDER__ po ON po.poname = pl.poname')->where( array( 'pl.sku' => $sku ) )->field('pl.poname,pl.name,pl.num,pl.date_purch,pl.earn13,po.warehouse,po.state')->select();
    	if( $data ) {
	    	foreach( $data as $key => $value ) {
	    		$data[$key]['date_purch'] = date( 'Y-m-d H:i:s' , $value['date_purch'] );
	    	}
    	}
    	exit( json_encode(array('data' => $data , 'status' => 1 ) ) );
    }

    /**
     * 进境清单
     * @return [type] [description]
     */
    public function getTransOrderLines(){
    	$sku = I('get.sku') ? I('get.sku') : exit( json_encode(array('msg' => '信息错误' , 'status' => 0 ) ) );
    	$data = M('transorder_lines')->alias('tl')->join('__TRANSORDER__ t ON t.toname = tl.toname')->where( array( 'tl.sku' => $sku ) )->field('t.toname,t.poname,t.warehouse_code,t.ttime,tl.qty,t.supplier,t.state')->select();
    	if( $data ) {
	    	foreach( $data as $key => $value ) {
	    		$data[$key]['ttime'] = date( 'Y-m-d H:i:s' , $value['ttime'] );
	    	}
    	}
    	exit( json_encode(array('data' => $data , 'status' => 1 ) ) );
    }

    /**
     * execl 下载
     */
    public function execlDown() {
    	$sku = I('get.sku') ? I('get.sku') : $this->error( '无效SKU' );
    	$xlsName  = "warning";
    	if( I('get.type') == 0 ) {
    		$xlsCell  = array(
					        array('channel_code','来源渠道'),
					        array('trade_pattern','贸易模式'),
					        array('ref','订单号'),
					        array('buyer','购买人'),
					        array('price','购买单价'),
					        array('num','购买数量'),
					        array('date_order','订单时间')  
				        );
    		$xlsData = M('order_line')->alias('ol')->join('__ORDER__ o ON o.id = ol.order_id')->where( array( 'ol.sku' => $sku ) )->field('o.ref,ol.price,ol.date_order,o.buyer,o.channel_code,ol.num,o.trade_pattern')->order('ol.date_order DESC')->select();
    	} else if( I('get.type') == 1) {
    		$xlsCell  = array(
					        array('picking_id','批次号'),
					        array('qty_act','库存变化'),
					        array('location_id','来源'),
					        array('location_dest_id','目的'),
					        array('procure_method','移动方向'),
					        array('state','状态'),
					        array('date','时间')  
				        );
    		$xlsData = M('stock_moveline')->where( array( 'sku' => $sku ) )->order('`date` DESC')->field('`picking_id`,`qty_act`,`location_id`,`location_dest_id`,`procure_method`,`state`,`date`')->select();
	        if( $xlsData ) {
		        foreach( $xlsData as $key => $value ) {
		        	$xlsData[$key]['date'] = date('Y-m-d H:i:s' , $value['date']);
		        	$xlsData[$key]['location_id'] = getStockStatus( $value['location_id'] );
		        	$xlsData[$key]['location_dest_id'] = getStockStatus( $value['location_dest_id'] );
		        }
	    	}
    	} else if( I('get.type') == 2 ) {
    		$xlsCell  = array(
					        array('earn13','earn13码'),
					        array('poname','采购单号'),
					        array('name','商品名称'),
					        array('num','采购数量'),
					        array('warehouse','目的地'),
					        array('state','状态'),
					        array('date_purch','采购时间')  
				        );
    		$xlsData = M('purchorder_line')->alias('pl')->join('__PURCHORDER__ po ON po.poname = pl.poname')->where( array( 'pl.sku' => $sku ) )->field('pl.poname,pl.name,pl.num,pl.date_purch,pl.earn13,po.warehouse,po.state')->select();
	    	if( $xlsData ) {
		    	foreach( $xlsData as $key => $value ) {
		    		$xlsData[$key]['date_purch'] = date( 'Y-m-d H:i:s' , $value['date_purch'] );
		    	}
	    	}
    	} else if( I('get.type') == 3 ) {
    		$xlsCell  = array(
					        array('toname','编号'),
					        array('supplier','供应商'),
					        array('warehouse_code','仓库'),
					        array('qty','数量'),
					        array('poname','采购单号'),
					        array('state','状态'),
					        array('ttime','时间')  
				        );
    		$xlsData = M('transorder_lines')->alias('tl')->join('__TRANSORDER__ t ON t.toname = tl.toname')->where( array( 'tl.sku' => $sku ) )->field('t.toname,t.poname,t.warehouse_code,t.ttime,tl.qty,t.supplier,t.state')->select();
	    	if( $xlsData ) {
		    	foreach( $xlsData as $key => $value ) {
		    		$xlsData[$key]['ttime'] = date( 'Y-m-d H:i:s' , $value['ttime'] );
		    	}
	    	}
    	}
    	$this->exportExcel( $xlsName , $xlsCell , $xlsData );
    }

}